package com.linkage.linkedlife.ui.activity

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.clark.dingding.adapter.NewsAdapter
import com.clark.dingding.constants.ARouterPath
import com.linkage.linkedlife.base.BaseActivity
import com.clark.dingding.interfaces.NewsItemClickCallBack
import com.clark.dingding.model.NewsData
import com.linkage.linkedlife.R
import com.linkage.linkedlife.databinding.ActivityNewsBinding
import com.linkage.linkedlife.viewmodel.NewsViewModel

/**
 * @ClassName: NewsActivity
 * @Description: java类作用描述
 * @Author: Clakr
 * @Date: 2020/12/16 0016 9:30
 */
@Route(path = ARouterPath.newsQuickActivity)
class NewsQuickActivity : BaseActivity() {

    lateinit var adapterNews: NewsAdapter
    lateinit var newsItemClickCallBack: NewsItemClickCallBack
    lateinit var activityNewsBinding: ActivityNewsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitle("消息列表")
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        activityNewsBinding = DataBindingUtil.setContentView(this, R.layout.activity_news)
        //        NewsViewModel newsViewModel = new NewsViewModel(ActivityNews.this.getApplication());
        /**使用ViewModelProvider才可以使ViewModel跟Activity的生命周期关联一起 */
        val newsViewModel: NewsViewModel =
            ViewModelProvider(this)[NewsViewModel::class.java]

        newsItemClickCallBack = object : NewsItemClickCallBack {
            override fun onClick(item: NewsData.ResultBean) {
                Toast.makeText(this@NewsQuickActivity, item.desc, Toast.LENGTH_SHORT).show();
            }

        }
        adapterNews = NewsAdapter(newsItemClickCallBack)
        var layoutManager = LinearLayoutManager(this)
        activityNewsBinding.newsRv.layoutManager = layoutManager
        activityNewsBinding.newsRv.adapter = adapterNews
        subscribeToModel(newsViewModel)

    }

    private fun subscribeToModel(model: NewsViewModel) {
        model.getLiveObservableData("15","1").observe(this, { newsData ->
            Log.d("clark", "data has changed")
            model.setUiObservableData(newsData!!)
//            newsData.results?.let { adapterNews.addData(it) }
            newsData.results?.let { adapterNews.setGirlsList(it) }
        })
    }


}